package com.zrl.ggkt.result;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @author: zrl
 * @date: 2022/8/9 18:14
 * @description:统一返回结果类
 */
@Data
public class R<T> {


    @ApiModelProperty(value = "返回码")
    private Integer code;

    @ApiModelProperty(value = "返回消息")
    private String message;

    @ApiModelProperty(value = "返回数据")
    private T data;

    public R(){}


    /*//成功的方法,无数据
    public static<T> Result<T> ok(){
        Result<T> result = new Result<>();
        result.setCode(200);
        result.setMsg("成功");
        result.setData(null);
        return result;
    }

    //失败的方法,无数据
    public static<T> Result<T> fail(){
        Result<T> result = new Result<>();
        result.setCode(201);
        result.setMsg("失败");
        return result;
    }*/

    /*//成功的方法,有数据
    public static<T> Result<T> ok(T data){
        Result<T> result = new Result<>();
        if (data!=null){
            result.setData(data);
        }
        result.setCode(200);
        result.setMessage("成功");
        return result;
    }

    //失败的方法,有数据
    public static<T> Result<T> fail(T data){

        Result<T> result = new Result<>();
        if (data!=null){
            result.setData(data);
        }
        result.setCode(201);
        result.setMessage("失败");
        return result;
    }*/

    public static <T> R<T> build(T data, Integer code, String message) {
        R<T> result = new R<T>();
        if (data != null) {
            result.setData(data);
        }
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    public static<T> R<T> ok(){
        return R.ok(null);
    }

    /**
     * 操作成功
     * @param data  baseCategory1List
     * @param <T>
     * @return
     */
    public static<T> R<T> ok(T data){
        return build(data,20000,"成功");
    }

    public static<T> R<T> fail(){
        return R.fail(null);
    }

    /**
     * 操作失败
     * @param data
     * @param <T>
     * @return
     */
    public static<T> R<T> fail(T data){
        return build(data, 20001,"失败");
    }

    public R<T> message(String msg){
        this.setMessage(msg);
        return this;
    }

    public R<T> code(Integer code){
        this.setCode(code);
        return this;
    }

}
